package hk.m4s.lr.repair.test.ui;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.LinearLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import framework.common.Constant;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.ProvinceModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.ui.anount.LoginActivity;
import okhttp3.Response;

public class WelcomeActivity extends AppCompatActivity {

    LinearLayout layout;
    private String permissionInfo;
    private final int SDK_PERMISSION_REQUEST = 127;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        layout = (LinearLayout) findViewById(R.id.layout);
        if (!(SharedPreferencesUtils.getSharedPreferences("proint", "") == null || SharedPreferencesUtils.getSharedPreferences("proint", "").equals(""))) {
            if (!SharedPreferencesUtils.getSharedPreferences("proint", "").equals("1")) {
                provincesAll();
            }
        } else {
            provincesAll();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
//                if(TextUtils.isEmpty(SharedPreferencesUtils.getSharedPreferences(Constant.splash,""))){
//                    jumpToSplashActivity();
//                }else{
                if (TextUtils.isEmpty(SharedPreferencesUtils.getSharedPreferences(Constant.token, ""))) {
                    jumpToLoginActivity();
                } else {
                    if (SharedPreferencesUtils.getSharedPreferences(Constant.check, "").equals("1")) {
                        jumpToMainActivity();
                    } else {
                        jumpToLoginActivity();
                    }
                }
                // }
            }
        }, 3000);
    }

    private void jumpToSplashActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void jumpToLoginActivity() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void jumpToMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    public void provincesAll() {
        if (!(SharedPreferencesUtils.getSharedPreferences(Constant.token, "") == null || SharedPreferencesUtils.getSharedPreferences(Constant.token, "").equals("")))
        {
            Map<String, Object> parmas = new HashMap<>();
            parmas.put("device_type", Constant.device_type);
            parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
            JSONObject jsondata = new JSONObject();
            parmas.put("jsonText", jsondata.toString());
            AccountSerive.provincesAll(WelcomeActivity.this, parmas, new ResponseCallback<String>() {
                @Override
                public void onSuccess(String accountModel) {

                }

                @Override
                public void onError(Response response, int errorCode, String errorMsg, Exception e) {

                }
            });
        }
    }

}
